package com.open.pokergames.biz.views.invite

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import com.open.pokergames.R
import com.open.pokergames.base.activity.LoadRecycleViewBaseActivity
import com.open.pokergames.base.entity.AppPageData
import com.open.pokergames.base.entity.PageReq
import com.open.pokergames.biz.entity.InviteEntity
import com.open.pokergames.biz.viewModel.BizViewModel
import com.open.pokergames.biz.views.invite.adapter.InviteListAdapter
import com.open.pokergames.comm.repository.cache.LocalCache
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import com.sinothk.widget.loadingRecyclerView.extend.LoadingRecycleViewHeader
import kotlinx.android.synthetic.main.activity_load_recycle_view_load_tip_white_bg_for_head.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class InviteMainActivity : LoadRecycleViewBaseActivity<InviteEntity>(), OnSuperListener<InviteEntity> {

    var viewModel: BizViewModel? = null
    private var adapter: InviteListAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_load_recycle_view_load_tip_white_bg_for_head)
        viewModel = BizViewModel()
        setViewTitle("邀请消息")
        initView()
    }

    override fun getLineDrawable(): Int {
        return R.drawable.list_divider_10
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)
        loadingRecyclerView.addHeaderView(createHeaderView());

        adapter = InviteListAdapter(this)
        adapter!!.setOnSuperListener(this)
        loadingRecyclerView.adapter = adapter
        loadingRecyclerView.setLoadingMoreEnabled(false)

        refreshData()
        loadingTipView.showLoading("努力加载中")
        loadingTipView.setLoadingViewBg(R.color.transparent)
        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }
    }

    override fun loadData(pageVo: PageVo<InviteEntity>) {
        val pageReq = PageReq<InviteEntity>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize

        val data = InviteEntity()
        data.toAccount = LocalCache.getUser().account
        pageReq.data = data
        viewModel!!.pageForInvite(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<InviteEntity>>) {

        if ("pageForInvite" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常",  R.drawable.data_empty)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<InviteEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.data.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.data_empty)
                loadingRecyclerView.setLoadingMoreEnabled(false)
                adapter!!.data = null
            } else {
                adapter!!.data = appPageData.data!!
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.data.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.data!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }


    override fun onClick(position: Int, itemData: InviteEntity?, flag: String?) {

        when (flag) {
            "addTv" -> {
                showLoadingDialog("正在处理")
                viewModel!!.inviteJoin(itemData!!.id, 1)
            }
            "cancelTv" -> {
                showLoadingDialog("正在处理")
                viewModel!!.inviteJoin(itemData!!.id, 2)
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun inviteJoinBusCallback(result: ResultInfo<InviteEntity>) {

        if ("inviteJoin" != result.eventType) {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                XUtils.toast().show("处理成功")
                refreshData()
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    private fun createHeaderView(): View {
        val headerView: View = LoadingRecycleViewHeader.getViewByLayoutId(this, R.layout.invite_main_header_view)

        val searchView = headerView.findViewById<LinearLayout>(R.id.searchView)
        searchView.setOnClickListener {
            XUtils.intent().openActivity(this@InviteMainActivity, InviteInputActivity::class.java).start()
        }
        return headerView
    }
}